package cn.com.shadowless.xinning;

import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import com.mengpeng.mphelper.ToastUtils;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsDownloader;
import com.tencent.smtt.sdk.TbsListener;
import com.xiaomi.channel.commonutils.logger.LoggerInterface;
import com.xiaomi.mipush.sdk.Logger;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.HashMap;

import cat.ereza.customactivityoncrash.config.CaocConfig;
import cn.com.shadowless.xinning.net.HttpMethods;
import cn.com.shadowless.xinning.receiver.MessageReceiver;
import cn.com.shadowless.xinning.utils.BadgeUtils;
import cn.com.shadowless.xinning.view.activity.OrderActivity;
import cn.jpush.android.api.JPushInterface;

/**
 * The type My application.
 *
 * @author sHadowLess
 */
public class MyApplication extends Application {

    private static MyApplication instance = null;
    private final String TAG = MyApplication.class.getSimpleName();
    private int activityNumber = 0;

    /**
     * The constant isSucceed.
     */
    public static boolean isSucceed = false;

    @Override
    public void onCreate() {
        super.onCreate();

        instance = this;

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                activityNumber++;
                Log.e(TAG, "onActivityStarted: " + activityNumber);
                if (Build.BRAND.equals(BadgeUtils.BRAND_HONOR) || Build.BRAND.equals(BadgeUtils.BRAND_HUA_WEI)) {
                    JPushInterface.clearAllNotifications(activity);
                    MessageReceiver.badgeCount = 0;
                    BadgeUtils.setBadge(0, instance);
                }
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {

            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                activityNumber--;
                Log.e(TAG, "onActivityStopped: " + activityNumber);
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                Log.e(TAG, "onActivityDestroyed: " + activityNumber);
                if (activityNumber == 0) {
                    JPushInterface.clearAllNotifications(instance);
                    BadgeUtils.setBadge(0, instance);
                    Log.e(TAG, "onActivityDestroyed: " + "已清除");
                }
            }
        });

        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        LoggerInterface newLogger = new LoggerInterface() {

            @Override
            public void setTag(String tag) {
                // ignore
            }

            @Override
            public void log(String content, Throwable t) {
                Log.e(TAG, content, t);
            }

            @Override
            public void log(String content) {
                Log.e(TAG, "log: " + content);
            }
        };
        Logger.setLogger(this, newLogger);

        ToastUtils.getInstance().initToast(this);

        HttpMethods.getInstance();

        initCrash();
    }

    private void initCrash() {
        CaocConfig.Builder.create()
                .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT)
                .enabled(true)
                .showErrorDetails(true)
                .showRestartButton(true)
                .logErrorOnRestart(true)
                .trackActivities(true)
                .minTimeBetweenCrashesMs(2000)
                .restartActivity(OrderActivity.class)
                .apply();
    }


    /**
     * Gets instance.
     *
     * @return the instance
     */
    public static MyApplication getInstance() {
        return instance;
    }

}
